import java.util.HashMap;

import soot.Unit;

/**
 * This class performs the widening operator.
 */
public class TrendAnalayzer {

	HashMap<Unit, MyAbstract> cmdToCurAbstract;
	HashMap<Unit, Integer> diffCount;
	int diffLimit;

	public TrendAnalayzer(int diffLimit) {
		cmdToCurAbstract = new HashMap<Unit, MyAbstract>();
		diffCount = new HashMap<Unit, Integer>();

		this.diffLimit = diffLimit;
	}

	public void analyzeAndWiden(Unit cmd, MyAbstract outState) {
		if (!cmdToCurAbstract.containsKey(cmd)) {
			MyAbstract temp = MyAbstract.copyCtor(outState);
			cmdToCurAbstract.put(cmd, temp);
			diffCount.put(cmd, 1);
			return;
		}

		MyAbstract existing = cmdToCurAbstract.get(cmd);
		MyAbstract temp = MyAbstract.copyCtor(existing);
		temp.widen(outState);

		if (!temp.equals(existing)) {
			diffCount.put(cmd, diffCount.get(cmd) + 1);
		}
		if (diffCount.get(cmd) > diffLimit) {
			outState.copy(temp);
		}
		cmdToCurAbstract.put(cmd, MyAbstract.copyCtor(outState));

	}
}
